import requests
import os
from time import sleep

file_name = "./我写的代码犯法吗直播视频.m3u8"
with open(file_name, 'r', encoding="utf-8") as f:
    my_str = f.read().splitlines()
# print(my_str)
down_url_list = []
for i in my_str:
    if i.startswith("/vhallyun/"):
        down_url_list.append(i)
# print(down_url_list)

host = "https://tehlsvodhls02.vhallyun.com"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36(KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}

save_folder = "./我写的代码犯法吗直播视频/"
if not(os.path.exists(save_folder)):
    os.mkdir(save_folder)
for i in range(len(down_url_list)):
    if i % 10 == 0:
        sleep(3)
    save_filename = down_url_list[i].rpartition("/")[2]
    download_url = host + down_url_list[i]
    res = requests.get(host + down_url_list[i], headers=headers)
    if res.status_code != 200:
        print("访问下载地址异常！[%s]" % download_url)
        continue
    with open(os.path.join(save_folder, save_filename), 'wb') as f:
        f.write(res.content)
        print("[%s] 下载完毕！" % save_filename)